##FIGURE D1##
validate<- lm(GoodBad~ hero, data=df_study2)
summary(validate)

validateplot<- df_study2 %>%
  data_grid(hero, .model = validate) %>%
  augment(validate, newdata = ., se_fit = TRUE) %>%
  ggplot(aes(x = hero, fill=hero)) +
  scale_fill_manual(values = colors) +
  scale_color_manual(values = colors)+
  geom_jitter(data=df_study2, aes(x=hero, y=GoodBad, fill=hero),
              height=.3, width=.2,size=3, alpha=.2,
              pch=21, color="grey57")+  
  theme_ggdist()+
  stat_dist_halfeye(
    aes(dist = dist_student_t(df = df.residual(validate), mu = .fitted, sigma = .se.fit)), 
    scale = .2, position = position_nudge(x = .35))+
  labs(title="Manipulation test: valence perception by treatment condition",
       x = "",
       y = "<<Target viewed as a hero -- Target viewed as a villain>>") +
  geom_hline(yintercept = .5, linetype="dashed")+
  guides(color=FALSE)+
  scale_x_discrete(labels=c("0" = "Negative valence\n(villain)", "1" = "Positive valence\n(hero)"))+
  theme(legend.position = "none",
        plot.title = element_text(color="black", face=2, hjust=0),
        axis.text.x = element_text(face=2),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank())+
  annotate("label", y = .22, x = 2, label = "Hero", size = 3, fontface = "bold", color = "#56B4E9")+
  annotate("label", y = .87, x = 1, label = "Villain", size = 3, fontface = "bold", color = "#C5407EFF")+
  coord_flip()

ggsave("figures_appendix/FigureD1.png", dpi=600)
validateplot

###FIGURE D2###
mod3<- lm(GoodBad~ fourarm, data=df_study2)
summary(mod3)
df_study2$predictedX<-predict(mod3, df_study2)

partyvalidate <- df_study2 %>%
  data_grid(fourarm, .model = mod3) %>%
  augment(mod3, newdata = ., se_fit = TRUE) %>%
  ggplot(aes(x = fourarm, fill=fourarm)) +
  scale_fill_manual(values = colors) +
  scale_color_manual(values = colors)+
  geom_jitter(data=df_study2, aes(x=fourarm, y=predictedX, fill=fourarm),
              height=.2, width=.25,size=3, alpha=.2,
              pch=21, color="grey57")+  
  theme_ggdist()+
  theme(plot.subtitle = element_text(face="bold"))+
  stat_dist_halfeye(
    aes(dist = dist_student_t(df = df.residual(mod3), mu = .fitted, sigma = .se.fit)), 
    scale = .3, position = position_nudge(x = .35))+
  geom_hline(yintercept = 0.50, linetype="dashed", colour="gray33")+
  labs(y="<<<Views as hero -- Views as villain>>>", x="Treatment condition",
       subtitle="b) No variation in perception of valence signal")+
  guides(color=FALSE)+
  scale_x_discrete(labels=c("0" = "Negative valence\nConservative voter", "1" = "Positive valence\nConservative voter",
                            "2" = "Negative valence\nLabour voter", "3" = "Positive valence\nLabour voter"))+
  theme(legend.position = "none",
        plot.title = element_text(color="black", face=2, hjust=0))+
  annotate("segment", x = 1.35, xend = 1.35, y = 0.6, yend = 0.8, color = "blue2", linetype = "dotted")+
  annotate("segment", x = 2.35, xend = 2.35, y = 0.6, yend = 0.3, color = "blue2", linetype = "dotted")+
  annotate("segment", x = 1.35, xend = 2.35, y = 0.6, yend = 0.6, color = "blue2", linetype = "dotted")+
  annotate("label", x = 1.85, y = 0.6, label = "0.63 ***", size = 3, fontface = "bold", color = "blue2")+
  annotate("text", x = 1.87, y = 0.65, label = "Conservative partisans", size = 3, fontface = "bold", color = "blue2")+
  annotate("segment", x = 3.35, xend = 3.35, y = 0.6, yend = 0.8, color = "red2", linetype = "dotted")+
  annotate("segment", x = 4.35, xend = 4.35, y = 0.6, yend = 0.3, color = "red2", linetype = "dotted")+
  annotate("segment", x = 3.35, xend = 4.35, y = 0.6, yend = 0.6, color = "red2", linetype = "dotted")+
  annotate("label", x = 3.85, y = 0.6, label = "0.66 ***", size = 3, fontface = "bold", color = "red2")+
  annotate("text", x = 3.85, y = .65, label = "Labour partisans", size = 3, fontface = "bold", color = "red2")

ggsave("figures_appendix/FigureD2.png", width = 18, height = 18, units = "cm")
partyvalidate


##TABLE D4##
main <- list()
main [['Organic model']] <- lm(partymatch ~ hero, data=df_study2)
main [['Prompted model']] <- lm(outcome_full ~ hero + guessparty, data=df_study2)
main [['Covariate-adj. model']] <- lm(outcome_full ~ hero + guessparty + resp_gender + degree +  age + agesq + nonwhite + queer + res_party2, data=df_study2)
msummary(main, star=TRUE, output='tables/Table_D4.tex', vcov="robust")

##TABLE D5##
placebo <- list()
placebo  [['Organic model']] <- lm(placebo ~ hero, data=df_study2)
placebo  [['Prompted model']] <- lm(placebo_full ~ hero + guesskids, data=df_study2)
placebo  [['Validation']] <- lm(GoodBad ~ hero, data=df_study2)
msummary(placebo, star=TRUE, output='tables/Table_D5.tex', vcov="robust")

##TABLE D6##
guesser<- list()
guesser  [['Guessed partisanship']] <-  lm(guessparty~ resp_gender + age + agesq + nonwhite +  queer + degree + partyID + affect_lab + affect_tory, data=df_study2)
guesser  [['Guessed children']] <-  lm(guesskids~ resp_gender + age + agesq + nonwhite +  queer + degree + partyID + affect_lab + affect_tory, data=df_study2)
msummary(guesser, star=TRUE, output='tables/Table_D6.tex', vcov="robust")

##TABLE E1##
recallers <- list()
recallers [['Organic model']] <-  lm(partymatch ~ hero*avg_recall, data=subset_df_study2)
recallers  [['Prompted model']] <-  lm(outcome_full ~ hero*avg_recall, data=df_study2)
msummary(recallers, star=TRUE, output='tables/Table_E1.tex', vcov="robust")

##TABLE E2##
party <- list()
party  [['Identity']] <- lm(outcome_full ~ hero*res_party2, data=df_study2)
party  [['# of children']] <- lm(placebo_full ~ hero*res_party2, data=df_study2)
party  [['Valence perception']] <- lm(GoodBad ~ hero*res_party2, data=df_study2)
msummary(party , star=TRUE, output='tables/Table_E2.tex', vcov="robust")

##TABLE E3##
affect <- list()
affect [['Identity']] <- lm(outcome_full ~ hero*outgroupdislike, data=df_study2)
affect  [['# of children']] <- lm(placebo_full ~ hero*outgroupdislike, data=df_study2)
affect [['Valence perception']] <- lm(GoodBad ~ hero*outgroupdislike, data=df_study2)
msummary(affect , star=TRUE, output='tables/Table_E3.tex', vcov="robust")

###FIGURE E1###

modelX<-lm (partymatch ~ hero*avg_recall, data=subset_df_study2)
summ(modelX, robust=TRUE)
subset_df_study2$predictINT<-predict(modelX, subset_df_study2)

modelXfull<-lm (outcome_full ~ hero*avg_recall, data=df_study2)
summ(modelXfull, robust=TRUE)
df_study2$predictINTfull<-predict(modelXfull, df_study2)

treat <- subset(subset_df_study2, hero== 1)
control <- subset(subset_df_study2, hero== 0)
treat_full <- subset(df_study2, hero== 1)
control_full <- subset(df_study2, hero== 0)

predplot1 <-
  ggplot(subset_df_study2, aes(avg_recall, partymatch, group = hero, color=hero)) +
  scale_color_manual(values = c("#C5407EFF", "#56B4E9")) +
  scale_fill_manual(values = c("#C5407EFF", "#56B4E9")) +
  theme_ggdist()+
  geom_hline(yintercept = 0.50, linetype="dashed", colour="gray33")+
  stat_smooth(method = "lm_robust", fullrange = TRUE, se=TRUE) +
  geom_jitter(data=treat, aes(x=avg_recall, y=predictINT, fill=hero), 
              alpha=.4, height=.04, width=1.2, size=2,
              color="#56B4E9")+
  geom_jitter(data=control, aes(x=avg_recall, y=predictINT, fill=hero), 
              alpha=.4, height=.04, width=1.2, size=2,
              color="#C5407EFF")+
  theme(legend.position = "none")+
  xlim(0, 1)+
  ylab("Pr(Project partisanship matches respondent partisanship)")+
  xlab("% of correct knowledge questions")+
  labs(subtitle="Organic projection")+
  theme(plot.subtitle = element_text(face="bold"))


predplot2 <-
  ggplot(df_study2, aes(avg_recall, outcome_full, group = hero, color=hero)) +
  scale_color_manual(values = c("#C5407EFF", "#56B4E9")) +
  scale_fill_manual(values = c("#C5407EFF", "#56B4E9")) +
  theme_ggdist()+
  geom_hline(yintercept = 0.50, linetype="dashed", colour="gray33")+
  stat_smooth(method = "lm_robust", fullrange = TRUE, se=TRUE) +
  geom_jitter(data=treat_full, aes(x=avg_recall, y=predictINTfull, fill=hero), 
              alpha=.4, height=.04, width=1.2, size=2,
              color="#56B4E9")+
  geom_jitter(data=control_full, aes(x=avg_recall, y=predictINTfull, fill=hero), 
              alpha=.4, height=.04, width=1.2, size=2,
              color="#C5407EFF")+
  theme(legend.position = "none")+
  xlim(0, 1)+
  ylab("")+
  xlab("% of correct knowledge questions")+
  labs(subtitle="Prompted projection")+
  annotate(
    geom="label", x = .75, y =.52, size =3, color="#56B4E9", fontface=2,
    label = "Slope for respondents in\npositive valence condition")+
  annotate(
    geom="label", x = .75, y =.48, size = 3, color="#C5407EFF", fontface=2,
    label = "Slope for respondents in\nnegative valence condition")+
  theme(plot.subtitle = element_text(face="bold"))

F_E1<-predplot1+predplot2

ggsave("figures_appendix/FigureE1.png",width =19, height = 24, units = "cm", dpi=700)
F_E1


###FIGURE D3###
library(quanteda)
library(quanteda.textstats)
library(quanteda.textplots)
library(tidytext)
library(textdata)


simple <- df_study2 %>%
  dplyr::select(opentext, hero, isLAB)

tidy_data <- simple %>% 
  unnest_tokens(word, opentext) %>%
  filter(str_detect(word, "[a-z]"))

tidy_labour <- subset(tidy_data, isLAB== 1)
tidy_tory <- subset(tidy_data, isLAB== 0)

text <- str_c(df_study2$opentext, collapse = "")

corp_responses<- corpus(df_study2, text_field="opentext")
dfm_weight <- corp_responses %>%
  tokens(remove_punct = TRUE) %>%
  dfm() %>%
  dfm_weight(scheme = "prop")


freq_weight <- textstat_frequency(dfm_weight, n = 20, 
                                  groups = dfm_weight$hero)

group.labs <- c("Villain", "Hero")
names(group.labs) <- c("0", "1")

treatments_dfm <- tokens(corp_responses, remove_punct = TRUE) %>%
  tokens_group(groups = hero) %>%
  dfm()

result_keyness <- textstat_keyness(treatments_dfm, target = "1")
textplot_keyness(result_keyness, show_reference = FALSE)
keynessplot<- textplot_keyness(result_keyness)+ 
  ggplot2:: scale_color_manual(name = "Treatment condition", labels = c("Positive valence (Hero)", "Negative valence (Villain)"),
                               values = c("#56B4E9","#C5407EFF"))+
  theme_ggdist()+
  labs(y="",
       title="Manipulation check: Relative keyness of terms by treatment condition")+
  xlim(-150, 250)+
  theme(legend.position =  c(0.75, 0.5),
        legend.title = element_blank(),
        legend.text = element_text(face="bold"),
        plot.title = element_text(color="black", face=2, hjust=0))

ggsave("figures_appendix/FigureD3.png", dpi=600)
keynessplot



###SUMMARY STATISTICS: TABLES D1-D2###


summary.df<- subset(df_study2, select = c(hero, resp_gender, age, queer, nonwhite, degree, IDstrength,
                                   partyID, outgroupdislike, RILEself, ideology_1, ideology_2, 
                                   affect_tory, affect_lab, affect_libdem, affect_greens,
                                   huddyfactor_outgroup, hud_outgroup_1a, hud_outgroup_2a, 
                                   hud_outgroup_3a, hud_outgroup_4a))
datasummary_skim(summary.df, type="categorical", output="tables/Table_D1.tex")
datasummary_skim(summary.df, output="tables/Table_D2.tex")


###TABLE D3###
table_result <- table(df_study2$isLAB, df_study2$hud_outgroup_1)
table_proportions <- prop.table(table_result, margin = 1)
table_df_study2 <- as.data.frame(table_proportions)
names(table_df_study2) <- c("Parisanship", "When people criticise the party\nit makes me feel good")
table_latex <- xtable(table_df_study2)
print(table_latex, include.rownames = TRUE, include.colnames = TRUE, file="tables/Table_D3a.tex")

table_result <- table(df_study2$isLAB, df_study2$hud_outgroup_2)
table_proportions <- prop.table(table_result, margin = 1)
table_df_study2 <- as.data.frame(table_proportions)
names(table_df_study2) <- c("Parisanship", "When I meet somebody who supports this party, I feel distant from this person")
table_latex <- xtable(table_df_study2)
print(table_latex, include.rownames = TRUE, include.colnames = TRUE, file="tables/Table_D3b.tex")

table_result <- table(df_study2$isLAB, df_study2$hud_outgroup_3)
table_proportions <- prop.table(table_result, margin = 1)
table_df_study2 <- as.data.frame(table_proportions)
names(table_df_study2) <- c("Parisanship", "When I speak about this party and its supporters, I refer to it as ‘their party’")
table_latex <- xtable(table_df_study2)
print(table_latex, include.rownames = TRUE, include.colnames = TRUE, file="tables/Table_D3c.tex")

table_result <- table(df_study2$isLAB, df_study2$hud_outgroup_4)
table_proportions <- prop.table(table_result, margin = 1)
table_df_study2 <- as.data.frame(table_proportions)
names(table_df_study2) <- c("Parisanship", "I get angry when people praise this party")
table_latex <- xtable(table_df_study2)
print(table_latex, include.rownames = TRUE, include.colnames = TRUE, file="tables/Table_D3d.tex")



